const zfdmUrl = 'http://192.168.168.33:8080/zfdm/syszfdm.json';
import { SysZfdm } from './tables/SysZfdm';
interface GetZfdmOptions {
	dmzl:string
	success:(result:SysZfdm[])=>void
}
let task = undefined;
const caches = {
	items: null,
	lastTime: new Date(),
	lastModified: 'Mon, 01 Jan 0001 00:00:00 UTC',
	options: [],
};
export function getSysZfdm(options:GetZfdmOptions) {
	const ti = new Date();
	if (caches.items == null || (ti.getTime() - caches.lastTime.getTime() > 60 * 1000)) {
		if (task == undefined) {
			task = uni.request({
				url: zfdmUrl,
				method: 'GET',
				dataType: 'json',
				header: { 'If-Modified-Since': caches.lastModified },
				success: (res) => {
					caches.lastTime = ti;
					if (res.statusCode == 200) {
						caches.items = res.data;
						caches.lastModified = res.header['last-modified'];
					} else if (res.statusCode != 304) {
						console.log(res.statusCode);
					}
					options.success(caches.items[options.dmzl] as SysZfdm[]);
					for (let i = 0; i < caches.options.length; i++) {
						caches.options[i].success(caches.items[caches.options[i].dmzl] as SysZfdm[]);
					}
					caches.options = [];
					task = undefined;
				},
				fail: (e) => {
					uni.showToast({
						title: '字符码获取失败',
					});
					caches.options = [];
					task = undefined;
				},
			});
		} else {
			caches.options.push(options);
		}
	} else {
		console.log(3);
		options.success(caches.items[options.dmzl] as SysZfdm[]);
	}
}
